<?php

/**
 * This is the model class for table "adminhtml_admin".
 *
 * The followings are the available columns in table 'adminhtml_admin':
 * @property integer $admin_id
 * @property integer $role_id
 * @property string $username
 * @property string $password
 * @property string $nickname
 * @property string $email
 * @property string $remark
 * @property string $create_time
 * @property string $update_time
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property AdminhtmlRole $role
 * @property AdminhtmlLog[] $adminhtmlLogs
 */
class AdminhtmlAdmin extends CommonAR {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return AdminhtmlAdmin the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function getResourceName() {
        return 'fsfs';
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'adminhtml_admin';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password', 'required'),
            array('role_id, status', 'numerical', 'integerOnly' => true),
            array('username', 'length', 'max' => 64),
            array('password', 'length', 'max' => 32),
            array('nickname', 'length', 'max' => 50),
            array('email', 'length', 'max' => 100),
            array('remark', 'length', 'max' => 255),
            array('create_time, update_time', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('admin_id, role_id, username, password, nickname, email, remark, create_time, update_time, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'role' => array(self::BELONGS_TO, 'AdminhtmlRole', 'role_id'),
            'adminhtmlLogs' => array(self::HAS_MANY, 'AdminhtmlLog', 'admin_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'admin_id' => 'Admin',
            'role_id' => 'Role',
            'username' => 'Username',
            'password' => 'Password',
            'nickname' => 'Nickname',
            'email' => 'Email',
            'remark' => 'Remark',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('admin_id', $this->admin_id);
        $criteria->compare('role_id', $this->role_id);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('nickname', $this->nickname, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('remark', $this->remark, true);
        $criteria->compare('create_time', $this->create_time, true);
        $criteria->compare('update_time', $this->update_time, true);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function loadByAccount($account = NULL) {
        if (!isset($account)) {
            throw new CHttpException(404, 'Can not find administrator by account.');
        } else {
            return $this->findByAttributes(array('username' => $account));
        }
    }

}